package com.ruoyi.web.controller.crm;

import com.bonuo.crm.domain.Faculty;
import com.bonuo.crm.service.BuyOrLaboratoryService;
import com.bonuo.crm.service.ContactService;
import com.bonuo.crm.service.FacultyService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

//院系Controller
@RestController
@RequestMapping("/crm/faculty")
@Api(tags = "院系管理")
public class FacultyController extends BaseController {
    @Autowired
    private FacultyService facultyService;
    @Autowired
    private BuyOrLaboratoryService buyOrLaboratoryService;
    @Autowired
    private ContactService contactService;

    //院系列表
    @ApiOperation(value = "查询院系列表",httpMethod = "GET")
    @GetMapping("/list")
    public TableDataInfo list(Faculty faculty,Integer pageNum,Integer pageSize) {
        if (pageNum != null && pageSize != null){
            startPage();
        }
        List<Faculty> list = facultyService.selectFacultyList(faculty);
        return getDataTable(list);
    }

    //院系添加
    @ApiOperation(value = "添加院系",httpMethod = "POST")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Faculty faculty) {
        Integer add = facultyService.insertFaculty(faculty);
        return toAjax(add);
    }

    //院系删除
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除院系",httpMethod = "DELETE")
    public AjaxResult delete(@PathVariable Integer id) {
        //查询当前院系下是否有已购设备/实验室现状信息
        Integer countBuyOrLaboratory = buyOrLaboratoryService.selectBuyOrLaboratoryByFacultyId(id);
        if(countBuyOrLaboratory >0) {
            return error("该院系下含有(已购设备/实验室现状)信息，请删除所有(已购设备/实验室现状)信息重试");
        }
        //查询当前院系下是否有联系人信息
        Integer countContact = contactService.selectContactByFacultyOrAgentId(id,0);
        if(countContact >0) {
            return error("该院系下含有联系人信息，请删除所有联系人信息重试");
        }

        Integer delete = facultyService.deleteFacultyById(id);
        return toAjax(delete);
    }

    //院系修改
    @PutMapping("/update")
    @ApiOperation(value = "修改院系",httpMethod = "PUT")
    public AjaxResult update(@RequestBody Faculty faculty) {
        Integer update = facultyService.updateFaculty(faculty);
        return toAjax(update);
    }

    //院系导出
    @PostMapping("/export")
    @ApiOperation(value = "导出院系数据",httpMethod = "POST")
    public void export(HttpServletResponse response, Faculty faculty) {
        List<Faculty> list = facultyService.selectFacultyList(faculty);
        ExcelUtil<Faculty> util = new ExcelUtil<Faculty>(Faculty.class);
        util.exportExcel(response, list, "院系数据");
    }
}
